import urllib
import time
import datetime
import settings

#-----------------------------------------------------------------------

def download(url):
    
    t = datetime.datetime.utcnow()
    timestr = str(time.mktime(t.timetuple()))
    filename="upload/%s.jpg" % (timestr.replace(".", "_"))
    
    file_path = "%s\\%s" % (settings.MEDIA_ROOT, filename)
    downloaded_image = file(file_path, "wb")

    image_on_web = urllib.urlopen(str(url))
    while True:
        buf = image_on_web.read(65536)
        if len(buf) == 0:
            break
        downloaded_image.write(buf)
    downloaded_image.close()
    image_on_web.close()
    
    return filename